/*
 * Copyright (C) 2011 Google Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.google.codemirror2_gwt.client;

import com.google.gwt.user.client.Command;

/**
 * An enumeration of the built-in CodeMirror commands, from codemirror.js:
 * http://s/?fileprint=//depot/google3/third_party/javascript/codemirror2/lib/codemirror.js
 * 
 * @author bsoares@google.com (Bobby Soares)
 */
public enum CodeMirrorCommand {
  
  DELETE_CHAR_LEFT("delCharLeft"),
  DELETE_CHAR_RIGHT("delCharRight"),
  DELETE_LINE("deleteLine"),
  DELETE_WORD_LEFT("delWordLeft"),
  DELETE_WORD_RIGHT("delWordRight"),
  GO_CHAR_LEFT("goCharLeft"),
  GO_CHAR_RIGHT("goCharRight"),
  GO_COLUMN_LEFT("goColumnLeft"),
  GO_COLUMN_RIGHT("goColumnRight"),
  GO_DOC_END("goDocEnd"),
  GO_DOC_START("goDocStart"),
  GO_LINE_DOWN("goLineDown"),
  GO_LINE_END("goLineEnd"),
  GO_LINE_START("goLineStart"),
  GO_LINE_START_SMART("goLineStartSmart"),
  GO_LINE_UP("goLineUp"),
  GO_PAGE_DOWN("goPageDown"),
  GO_PAGE_UP("goPageUp"),
  GO_WORD_LEFT("goWordLeft"),
  GO_WORD_RIGHT("goWordRight"),
  INDENT_AUTO("indentAuto"),
  INDENT_MORE("indentMore"),
  INDENT_LESS("indentLess"),
  INSERT_TAB("insertTab"),
  KILL_LINE("killLine"),
  NEW_LINE_AND_INDENT("newlineAndIndent"),
  REDO("redo"),
  SELECT_ALL("selectAll"),
  TOGGLE_OVERWRITE("toggleOverwrite"),
  TRANSPOSE_CHARS("transposeChars"),
  UNDO("undo");

  private final String id;

  private CodeMirrorCommand(String id) {
    this.id = id;
  }

  @Override public String toString() {
    return id;
  }

  /**
   * Obtains the executable command targeting the specified editor.
   */
  public Command getCommand(final CodeMirrorWrapper editor) {
    return new Command() {
      @Override public void execute() {
        execute(editor, id);
      }

      private final native void execute(CodeMirrorWrapper editor, String id) /*-{
        $wnd.CodeMirror.commands[id](editor);
      }-*/;
    };
  }
}
